package kd.bos.kdec.pincar;

import java.text.NumberFormat;
import java.util.EventObject;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.filter.FilterParameter;
import kd.bos.form.control.Label;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.BillList;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 部门低碳排行
 * 
 * @author zhangyizhe
 *
 */
public class PinCarForMobReportlist extends AbstractFormPlugin {

	private String[] buttons = new String[] { "" };

	@Override
	public void registerListener(EventObject e) {
		super.registerListener(e);
		this.addClickListeners(this.buttons);
	}

	@Override
	public void click(EventObject evt) {
		super.click(evt);
	}

	@Override
	public void beforeClick(BeforeClickEvent evt) {
		super.beforeClick(evt);

	}

	@Override
	public void afterBindData(EventObject e) {
		super.afterBindData(e);
		long orgId = RequestContext.get().getOrgId();
		long userId = Long.parseLong(RequestContext.get().getUserId());
		QFilter orfilter = new QFilter("orgfield", QCP.equals, orgId);
		DynamicObject[] objs = BusinessDataServiceHelper.load("kdec_pincar_reportbill", "creator,kmsnum",
				orfilter.toArray(), "kmsnum desc");
		int mypm = 1;
		for (DynamicObject dyObj : objs) {
			DynamicObject creator = (DynamicObject) dyObj.get("creator");
			Long id = creator.getLong("masterid");
			if (userId != id) {
				mypm = mypm + 1;
			} else if (userId == id) {
				break;
			}
		}
		((Label) this.getControl("mypm")).setText(mypm + "");
		int allCount = objs.length;
		// 创建一个数值格式化对象
		NumberFormat numberFormat = NumberFormat.getInstance();
		// 设置精确到小数点后2位
		numberFormat.setMaximumFractionDigits(1);
		String result = numberFormat.format((float) (allCount-mypm+1) / (float) allCount * 100);
		String fstar = result + "";
		Label labelpc = this.getControl("mypc");
		labelpc.setText(fstar);
		QFilter filter = new QFilter("creator", QCP.equals, userId);
		DynamicObject object = BusinessDataServiceHelper.loadSingleFromCache("kdec_pincar_reportbill", "kmsnum",
				filter.toArray());
		Label label = this.getControl("myjf");
		label.setText(object.getString("kmsnum"));
	}

	@Override
	public void beforeBindData(EventObject e) {
		super.beforeBindData(e);
		// 根据组织隔离数据
		long orgId = RequestContext.get().getOrgId();
		// 加载单据列表添加筛选条件
		BillList billList = this.getControl("billlistap");
		FilterParameter filterParameter = new FilterParameter();
		QFilter filter = new QFilter("orgfield", QCP.equals, orgId);
		filterParameter.setFilter(filter);
		billList.setFilterParameter(filterParameter);
	}
}
